<?php

namespace Xin\EasyQueue;

use Hyperf\Context\ApplicationContext as HyperfApplicationContext;
use support\Container as WebmanContainer;
use Xin\EasyQueue\Contracts\Dispatcher;
use Xin\EasyQueue\Hyperf\RedisDispatcher as HyperfRedisDispatcher;
use Xin\EasyQueue\Webman\RedisDispatcher as WebmanRedisDispatcher;
use Xin\Support\Framework;

class Initializer
{
    /**
     * @var bool
     */
    protected static $inited = false;


    /**
     * @return void
     */
    public static function run()
    {
        if (self::$inited) {
            return;
        }

        self::$inited = true;

        Framework::when()->hyperf(function () {
            /** @var \Hyperf\Di\Container $container */
            $container = HyperfApplicationContext::getContainer();
            $container->define(Dispatcher::class, function ($container) {
                return $container->make(HyperfRedisDispatcher::class);
            });
        })->webman(function () {
            /** @var \DI\Container $container */
            $container = WebmanContainer::instance();
            $container->set(Dispatcher::class, function ($container) {
                return $container->make(WebmanRedisDispatcher::class);
            });
        })->invoke();
    }
}
